package com.yaorange.module.system.controller;

import com.yaorange.module.system.service.SysRoleService;
import com.yaorange.pojo.SysRole;
import com.yaorange.response.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    /**
     * 获取角色列表
     */
    @GetMapping
    public Result<List<SysRole>> listRoles() {
        List<SysRole> sysRoleList = sysRoleService.findAllRoles();
        return Result.success(sysRoleList);
    }

    /**
     * 根据编号获取角色
     */
    @GetMapping("/{id}")
    public Result<SysRole> getRoleById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.findById(id);
        return Result.success(sysRole);
    }

    /**
     * 新增角色
     */
    @PostMapping
    public Result<?> createUser(@Valid @RequestBody SysRole user) {
        sysRoleService.save(user);
        return Result.success();
    }

    /**
     * 修改角色
     */
    @PutMapping()
    public Result<?> updateUser( @Valid @RequestBody SysRole roleDetails) {
        sysRoleService.update(roleDetails);
        return Result.success();
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteUser(@PathVariable Long id) {
        sysRoleService.deleteById(id);
        return Result.success();
    }

    /**
     *  根据角色列表批量删除角色
     */
    @DeleteMapping
    public Result<?> deleteUsers(@RequestBody List<Long> ids) {
        sysRoleService.deleteByIds(ids);
        return Result.success();
    }

    /**
     *  获取角色的权限列表
     * @param roleId
     * @return
     */
    @GetMapping("/{roleId}/permission")
    public Result<List<Long>> getPermissionIdsByRoleId(@PathVariable("roleId") Long roleId){
        return Result.success(sysRoleService.findPermissionIdsByRoleId(roleId));
    }


    /**
     *  为指定角色授权权限
     */
    @PostMapping("/{roleId}/permission")
    public Result<?> grantPermissions(@RequestBody List<Long> permissionIds, @PathVariable("roleId") Long roleId){
        sysRoleService.grantPermissions(permissionIds,roleId);
        return Result.success();
    }



}
